require 'View/UIBase/UIBase'

UIRuneList = UIBase.New(PanelNames.UIRuneList)
local specialRuneIndex = 6  --天穹符文index

function UIRuneList.Init(partnerId, slotIndex)
    UIRuneList.partnerId = partnerId
    UIRuneList.slotIndex = slotIndex
    UIBase.Init(UIRuneList)
end

function UIRuneList:OnCreate()
    self.btnClose = self:GetChild("bgclose")
    self.comItem = self:GetChild("equipListitem")
    self.item = self.comItem:GetChild("equipItem")
    self.txtName = self.comItem:GetChild("name")
    self.txtPower = self.comItem:GetChild("attack")
    self.listRune = self:GetChild("List_Equipment")
    self.txtTitle = self:GetChild("title")
    self.ctrlHaveItem = self:GetController("c1")
    self.ctrlShowChange = self:GetController("tips")
    self.ctrlListNothing = self:GetController("nothing")
    self.ctrlTips = self:GetController("tips")
    self.btnSure = self:GetChild("btn_sure")
    self.btnCancel = self:GetChild("btn_cancel")
    self.txtTips = self:GetChild("tipText")
    self.item1 = self:GetChild("equip1")
    self.item2 = self:GetChild("equip2")
    self.item1HeroIcon = self:GetChild("equip1hero"):GetChild("HeadIcon")
    self.item2HeroIcon = self:GetChild("equip2hero"):GetChild("HeadIcon")
    self:AddEvent()
    self:InitView()
    self:Refresh()
end

function UIRuneList:AddEvent()
    self:RegisterOnClick(self.btnClose,self.OnClickClose)
    self:RegisterOnClick(self.btnItem,self.OnClickItem)
    self:SetDynamicList(self.listRune ,self.RuneRender)
    self:RegisterEvent(Config.EventType.FreshHeroRune,self.Refresh)
    self:RegisterEvent(Config.EventType.FreshHeroRuneExchange,self.Refresh)
end

function UIRuneList:InitView()
    self.ctrlShowChange.selectedIndex = 0
    self.ctrlTips.selectedIndex = 0
    local runeInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId, self.slotIndex)
    if runeInfo then
        self.txtTitle.text = "符文替换"
    else
        self.txtTitle.text = "符文镶嵌"
    end
end

function UIRuneList:Refresh()
    local runeInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId, self.slotIndex)
    if runeInfo then
        self.txtName.text = runeInfo.name
        self.txtPower.text = runeInfo.power
        logicMgr.RuneManager.SetRuneItemData(self.item,runeInfo)
        self.ctrlHaveItem.selectedIndex = 1
    else
        self.ctrlHaveItem.selectedIndex = 0
    end

    self.runeList = self:GetShowRuneList()
    if self.runeList and #self.runeList > 0 then
        self.ctrlListNothing.selectedIndex = 0
    else
        self.ctrlListNothing.selectedIndex = 1
    end
    self.listRune.numItems = #self.runeList
end

function UIRuneList:GetShowRuneList()
    local heroData = logicMgr.HeroManager.getHeroDatabyPartnerId(self.partnerId,true)
    local changeShowList = logicMgr.RuneManager.GetCanChangeRuneList(heroData.partnerId, self.slotIndex) or {}
    local stuffShowList = logicMgr.RuneManager.GetCanWearRuneList(heroData.partnerId, self.slotIndex) or {}
    local showList = {}
    if self.slotIndex == specialRuneIndex then
        for k,v in pairs(stuffShowList) do
            if v.commonType == 2 then
                table.insert(showList, v)
            end
        end
        for k,v in pairs(changeShowList) do
            if v.commonType == 2 then
                table.insert(showList, v)
            end
        end
    else
        for k,v in pairs(stuffShowList) do
            if v.commonType == 1 then
                table.insert(showList, v)
            end
        end
        for k,v in pairs(changeShowList) do
            if v.commonType == 1 then
                table.insert(showList, v)
            end
        end
    end
    return showList
end

function UIRuneList:RuneRender(index, go)
    local runeInfo = self.runeList[index]
    if runeInfo then
        local btnItem = go:GetChild("equipItem")
        local btnReplace = go:GetChild("btn_replace")
        local btnEquip = go:GetChild("btn_unreplace")
        local txtName = go:GetChild("name")
        local txtPower = go:GetChild("attack")
        local imgHero = go:GetChild("hero"):GetChild("HeadIcon")
        local ctrlType = go:GetController("equip")
        logicMgr.RuneManager.SetRuneItemData(btnItem, runeInfo, true)
        txtName.text = runeInfo.name
        txtPower.text = runeInfo.power
        local _heroData = logicMgr.HeroManager.getHeroDatabyPartnerId(runeInfo.partnerId,true)
        if runeInfo.partnerId and _heroData then
            ctrlType.selectedIndex = 0
            local url = "ui://ICONAtlas/".._heroData.icon
            imgHero.url = url
        else
            ctrlType.selectedIndex = 1
            imgHero.url = ""
        end
        btnReplace.onClick:Clear()
        btnEquip.onClick:Clear()
        btnReplace.onClick:Add(function()
            self.ctrlTips.selectedIndex = 1
            self:RefreshTips(runeInfo.partnerId, runeInfo.index)
        end)
        btnEquip.onClick:Add(function()
            runeHandler.reqInlayRune(self.partnerId, runeInfo.itemId, self.slotIndex)
        end)
    end
end

function UIRuneList:RefreshTips(partnerId, index)
    self.btnSure.onClick:Clear()
    self.btnSure.onClick:Add(function()
        runeHandler.reqChangeRune(self.partnerId, self.slotIndex, partnerId, index)
        self.ctrlTips.selectedIndex = 0
    end)
    self.btnCancel.onClick:Clear()
    self.btnCancel.onClick:Add(function()
        self.ctrlTips.selectedIndex = 0
    end)
    local myHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(self.partnerId)
    local otherHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(partnerId)
    local otherRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(partnerId, index)
    local myRuneInfo = logicMgr.RuneManager.GetHeroRuneSrvInfo(self.partnerId, self.slotIndex)
    logicMgr.RuneManager.SetRuneItemData(self.item1,myRuneInfo)
    logicMgr.RuneManager.SetRuneItemData(self.item2,otherRuneInfo)
    if myRuneInfo and myRuneInfo["partnerId"] and otherRuneInfo["partnerId"] then
        self.item1HeroIcon.url = EnumConst.AtlasPath.ICONAtlas..otherHeroData.icon
        self.item2HeroIcon.url = EnumConst.AtlasPath.ICONAtlas..myHeroData.icon
    else
        logicMgr.RuneManager.SetRuneItemData(self.item1,otherRuneInfo)
        self.item1HeroIcon.url = EnumConst.AtlasPath.ICONAtlas..otherHeroData.icon
        self.item2HeroIcon.url = EnumConst.AtlasPath.ICONAtlas..myHeroData.icon
    end
    self.txtTips = self:GetChild("tipText")
    --是否要将 【%s】 装备的 【%s】 替换到 【%s】 身上？
    local _tipStr = getLanguage("UIEquip_IsReplaceEquip")
    local itemName = getLanguage(dataTable.getDataTableByStringId("t_item", otherRuneInfo.itemModelId,"f_ItemName"))
    _tipStr = string.format(_tipStr,otherHeroData.name,itemName,myHeroData.name)
    self.txtTips.text = _tipStr
end

function UIRuneList:OnClickClose()
    closeUI(PanelNames.UIRuneList)
end

return UIRuneList